package com.sanqing.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sanqing.dao.base.BaseDao;
import com.sanqing.po.Course;
import com.sanqing.po.Examine;
import com.sanqing.po.ExamineBatch;
import com.sanqing.po.Questions;
import com.sanqing.po.Subject;
import com.sanqing.service.ExamineService;
import com.sanqing.util.Page;
import com.sanqing.util.PageResult;
import com.sanqing.util.PageUtil;

@Service("examineService")
@Transactional(readOnly = true)
public class ExamineServiceImpl implements ExamineService {

	@Resource(name = "examineDao")
	private BaseDao<Examine, String> examineDao;

	@Resource(name = "subjectDao")
	private BaseDao<Subject, String> subjectDao;

	@Resource(name = "questionsDao")
	private BaseDao<Questions, String> questionsDao;

	@Resource(name = "examineBatchDao")
	private BaseDao<ExamineBatch, String> examineBatchDao;

	@Override
	public PageResult queryExamineByPage(Page page, Examine examine) {
		Map<String, Object> map = new HashMap<String, Object>();
		String hql = "from Examine a where 1=1";

		if (StringUtils.isNotBlank(examine.getExamineName())) {
			hql += " and a.examineName like:examineName";
			map.put("examineName", "%" + examine.getExamineName() + "%");
		}

		page = PageUtil.createPage(page.getEveryPage(), (int) examineDao.countHqlResult(hql, map), page.getCurrentPage());// 根据总记录数创建分页信息
		List<Examine> list = examineDao.findByPage(page, hql, map);// 通过分页信息取得试题
		PageResult result = new PageResult(page, list);// 封装分页信息和记录信息，返回给调用处
		return result;
	}

	@Override
	@Transactional(readOnly = false)
	public void add(Examine examine) {
		examineDao.save(examine);
		ExamineBatch batch = new ExamineBatch();
		batch.setExamine(examine);
		batch.setStartTime(examine.getStartTime());
		batch.setStopTime(examine.getStopTime());
		batch.setName(examine.getExamineName());
		examineBatchDao.save(batch);
	}

	@Override
	public Examine getById(String id) {
		return examineDao.getByKey(id);
	}

	@Override
	@Transactional(readOnly = false)
	public void edit(Examine examine) {
		Examine r = examineDao.getByKey(examine.getId());
		BeanUtils.copyProperties(examine, r, new String[] {"startTime","stopTime"});
	}

	@Override
	@Transactional(readOnly = false)
	public void delete(String id) {
		Examine r = examineDao.getByKey(id);
		Set<ExamineBatch> batchs = r.getExamineBatchs();
		if(batchs!=null && batchs.size()>0){
			for (ExamineBatch batch : batchs) {
				examineBatchDao.delete(batch);
			}
		}
		examineDao.delete(id);
		
	}

	@Override
	public List<Examine> getList() {
		return examineDao.find("from Examine a");
	}

	@Override
	public List<Subject> getSubject(String subjectType, Course course) {
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("subjectType", subjectType);
		values.put("courseId", course.getCourseId());
		String hql = "from Subject a where a.subjectType=:subjectType and a.course.courseId=:courseId";
		Query query = subjectDao.createQuery(hql, values);
		return query.list();
	}

	@Override
	@Transactional(readOnly = false)
	public void customPaper(Examine examine, String[] subjectID) {
		if (subjectID != null && subjectID.length > 0) {
			for (int i = 0; i < subjectID.length; i++) {
				Questions q = new Questions();
				q.setExamine(examine);
				q.setSubject(new Subject(subjectID[i]));
				questionsDao.save(q);
			}
		}
	}

	@Override
	public List<Questions> getExamineSubject(String subjectType, Course course) {
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("subjectType", subjectType);
		values.put("courseId", course.getCourseId());
		String hql = "from Questions a where a.subject.subjectType=:subjectType and a.examine.course.courseId=:courseId";
		Query query = questionsDao.createQuery(hql, values);
		return query.list();
	}

}
